﻿using AntdUI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TPressure
{
    public partial class SelectDevicePointForm : BaseForm
    {
        #region 自定义方法

        /// <summary>设备点位列表
        /// 
        /// </summary>
        public void LoadDevicePointList()
        {
            List<DevicePoint> list = SqlSugarHelper.Inquireable<DevicePoint>(null);
            UpdateTable(list);
        }

        /// <summary>更新DataGridView
        /// 
        /// </summary>
        /// <param name="list"></param>
        private void UpdateTable(List<DevicePoint> list)
        {
            var antItemList = new List<DevicePointItem>();
            foreach (DevicePoint item in list)
            {
                string range = item.range_min.ToString() + "-" + item.range_max.ToString();
                string outValue = item.out_min.ToString() + "-" + item.out_max.ToString();
                string alarm = item.alarm_hight.ToString() + "-" + item.alarm_low.ToString();
                string register_type = intToRegister(item.register_type);
                antItemList.Add(new DevicePointItem(item.device_number, item.device_name, item.point_number, item.point_name, item.point_address, range, outValue, item.amend, alarm, item.error_range, item.unit, register_type));
            }
            Table1.DataSource = antItemList;
        }

        private string intToRegister(int type)
        {
            string registerType = "";
            if (type == 1)
            {
                registerType = "DI";
            }
            else if (type == 2)
            {
                registerType = "DO";
            }
            else if (type == 3)
            {
                registerType = "InputRegister";
            }
            else if (type == 4)
            {
                registerType = "HoldingRegister";
            }
            return registerType;
        }

        #endregion


        #region 系统方法
        public SelectDevicePointForm()
        {
            InitializeComponent();
        }

        private void SelectDevicePointForm_Load(object sender, EventArgs e)
        {
            Table1.Columns = new AntdUI.ColumnCollection {
                new AntdUI.Column("device_number","设备编号"){ColAlign=AntdUI.ColumnAlign.Center},
                new AntdUI.Column("device_name","设备名称"){ColAlign=AntdUI.ColumnAlign.Center},
                new AntdUI.Column("point_number","点位编号"){ColAlign=AntdUI.ColumnAlign.Center},
                new AntdUI.Column("point_name","点位名称"){ColAlign=AntdUI.ColumnAlign.Center},
                new AntdUI.Column("point_address","点位地址"){ColAlign=AntdUI.ColumnAlign.Center},
                new AntdUI.Column("range","量程范围"){ColAlign=AntdUI.ColumnAlign.Center},
                new AntdUI.Column("outValue","输出范围"){ColAlign=AntdUI.ColumnAlign.Center},
                new AntdUI.Column("alarm","报警范围"){ColAlign=AntdUI.ColumnAlign.Center},
                new AntdUI.Column("error_range","判断范围"){ColAlign=AntdUI.ColumnAlign.Center},
                new AntdUI.Column("unit","数据单位"){ColAlign=AntdUI.ColumnAlign.Center},
                new AntdUI.Column("selected","通道选择"){ColAlign=AntdUI.ColumnAlign.Center},
            };
            Spin.Config config = new Spin.Config
            {
                Text = "正在加载中...",
            };
            Table1.Spin(config, () =>
            {
                LoadDevicePointList();
            }, () =>
            {

            });
        }

        private void Table1_CellDoubleClick(object sender, TableClickEventArgs e)
        {
            DevicePointItem item = (DevicePointItem)e.Record;
            FormPointManageForm frm = (FormPointManageForm)this.Owner;
            frm.devicePointitem = item;
            frm.freshDevicePointInfo();
            this.Close();
        }

        #endregion

    }
}
